//
//  RiskAlertView.m
//  HengYouCai
//
//  Created by lwj on 2017/9/16.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "RiskAlertView.h"
#import <Masonry/Masonry.h>
@implementation RiskAlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title type:(NSString *)types desc:(NSString *)desc otherDesc:(NSString *)otherDesc firstBtn:(NSString *)firstBtnTitle secondBtn:(NSString *)secondBtnTitle firsBlock:(firstBlock)firBlock seconBlock:(secondBlock)secBlock
{
    return [[RiskAlertView alloc] initWithFrame:frame title:title type:types desc:desc otherDesc:otherDesc firstBtn:firstBtnTitle secondBtn:secondBtnTitle firsBlock:firBlock seconBlock:(secondBlock)secBlock];
}
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title type:(NSString *)types desc:(NSString *)desc otherDesc:(NSString *)otherDesc firstBtn:(NSString *)firstBtnTitle secondBtn:(NSString *)secondBtnTitle firsBlock:(firstBlock)firBlocks seconBlock:(secondBlock)secBlocks
{
    if (self = [super initWithFrame:frame]) {
        //
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        [window addSubview:self];
        
        self.backgroundColor = [UIColor colorWithWhite:.2 alpha:.5];
        
        _oneBlock = firBlocks;
        _twoBlock = secBlocks;
        
        if (otherDesc.length>1&&desc.length>1) {
            UIView *alertView =  [[NSBundle mainBundle] loadNibNamed:@"RiskOtherAlertView" owner:nil options:nil].firstObject;
            
            alertView.layer.cornerRadius = 10;
            
            [self addSubview:alertView];
            
            [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(265, 214));
                make.centerX.mas_equalTo(self);
                make.centerY.mas_equalTo(self);
                
            }];
            
            UILabel *firstLab = [alertView viewWithTag:1001];
            UILabel *secondLab = [alertView viewWithTag:1002];
            UILabel *thirdLab = [alertView viewWithTag:1003];
            UILabel *fouthLab = [alertView viewWithTag:1004];
            UIButton *firBtn = [alertView viewWithTag:1005];
            UIButton *secBtn = [alertView viewWithTag:1006];
            firstLab.text = title;
            secondLab.text = types;
            thirdLab.text = desc;
            fouthLab.text = otherDesc;
            [firBtn setTitle:firstBtnTitle forState:UIControlStateNormal];
            [secBtn setTitle:secondBtnTitle forState:UIControlStateNormal];
            [[firBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];
                _oneBlock();
            }];
            [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];

                _twoBlock();
            }];


        }else if (desc.length<1) {
            UIView *alertView =  [[NSBundle mainBundle] loadNibNamed:@"RiskAlertBeyondView" owner:nil options:nil].firstObject;
            
            alertView.layer.cornerRadius = 10;
            
            [self addSubview:alertView];
            
            [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(265, 160));
                make.centerX.mas_equalTo(self);
                make.centerY.mas_equalTo(self);
                
            }];
            
            UILabel *firstLab = [alertView viewWithTag:1001];
            UILabel *secondLab = [alertView viewWithTag:1002];
            UILabel *fouthLab = [alertView viewWithTag:1004];
            UIButton *firBtn = [alertView viewWithTag:1005];
            UIButton *secBtn = [alertView viewWithTag:1006];
            firstLab.text = title;
            secondLab.text = types;
            fouthLab.text = otherDesc;
            [firBtn setTitle:firstBtnTitle forState:UIControlStateNormal];
            [secBtn setTitle:secondBtnTitle forState:UIControlStateNormal];
            [[firBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];
                _oneBlock();
            }];
            [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];
                
                _twoBlock();
            }];
            
            
        } else if (firstBtnTitle.length<1){
        
            UIView *alertView =  [[NSBundle mainBundle] loadNibNamed:@"RiskAlertOneBtnView" owner:nil options:nil].firstObject;
            
            alertView.layer.cornerRadius = 10;
            
            [self addSubview:alertView];
            
            [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
                //            make.top.mas_equalTo(_bgView.mas_bottom).offset(15);
                make.size.mas_equalTo(CGSizeMake(266, 187));
                make.centerX.mas_equalTo(self);
                make.centerY.mas_equalTo(self);
                
            }];
            
            UILabel *firstLab = [alertView viewWithTag:1001];
            UILabel *secondLab = [alertView viewWithTag:1002];
            UILabel *thirdLab = [alertView viewWithTag:1003];
            UIButton *secBtn = [alertView viewWithTag:1005];
            firstLab.text = title;
            secondLab.text = types;
            thirdLab.text = desc;
            [secBtn setTitle:secondBtnTitle forState:UIControlStateNormal];
          
            [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];
                
                _twoBlock();
            }];

        } else{
            UIView *alertView =  [[NSBundle mainBundle] loadNibNamed:@"RiskAlertViews" owner:nil options:nil].firstObject;
            
            alertView.layer.cornerRadius = 10;
            
            [self addSubview:alertView];
            
            [alertView mas_makeConstraints:^(MASConstraintMaker *make) {
                //            make.top.mas_equalTo(_bgView.mas_bottom).offset(15);
                make.size.mas_equalTo(CGSizeMake(266, 187));
                make.centerX.mas_equalTo(self);
                make.centerY.mas_equalTo(self);
                
            }];
            
            UILabel *firstLab = [alertView viewWithTag:1001];
            UILabel *secondLab = [alertView viewWithTag:1002];
            UILabel *thirdLab = [alertView viewWithTag:1003];
            UIButton *firBtn = [alertView viewWithTag:1004];
            UIButton *secBtn = [alertView viewWithTag:1005];
            firstLab.text = title;
            secondLab.text = types;
            thirdLab.text = desc;
            [firBtn setTitle:firstBtnTitle forState:UIControlStateNormal];
            [secBtn setTitle:secondBtnTitle forState:UIControlStateNormal];
            [[firBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];

                _oneBlock();
            }];
            [[secBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                [self removeFromSuperview];

                _twoBlock();
            }];
        }
        
        
    }
    return self;
}
- (void)setUI
{
    
}
@end
